<!--
 * @Description: 
 * @Version: 2.0
 * @Author: Xing
 * @Date: 2021-02-05 22:32:42
 * @LastEditors: Xing
 * @LastEditTime: 2021-02-06 21:54:19
-->
<template>
  <div id="app">
    <HelloWord />
    <img alt="Vue logo" src="./assets/logo.png" />
    <h1>{{ count }}</h1>
    <h1>{{ double }}</h1>
    <button @click="increase">👍+1</button>
    <button @click="updateGreeting">改变标题</button>
    <div>坐标 x:{{ x }} y:{{ y }}</div>
    <h1 v-if="loading">loading!...</h1>
    <img class="dog-img" v-if="loaded" :src="result.message" alt="" srcset="" />
    <Modal :isOpen="modalIsOpen" @close-modal="onModalClose">My Modal</Modal>
    <div><button @click="openModal">展示modal</button></div>

    <!-- 展示异步请求标签 -->
    <Suspense>
      <template #default>
        <!-- <async-show /> -->
        <dog-show />
      </template>
      <template #fallback>
        <h1>async-show loading...</h1>
      </template>
    </Suspense>
  </div>
</template>

<script lang="ts">
import { ref, computed, reactive, toRefs, onMounted, onUpdated, watch, onUnmounted } from 'vue';
import HelloWord from './components/HelloWorld.vue';
import Modal from './components/Modal.vue';
import AsyncShow from './components/AsyncShow.vue';
import DogShow from './components/DogShow.vue';
import useMousePosition from './hooks/useMousePosition';
import useURLLoader from './hooks/useURLLoader';
interface DataProps {
  count: number;
  double: number;
  increase: () => void;
  numbers: number[];
  person: { name?: string };
}
interface DogResult {
  message: string;
  status: string;
}
export default {
  name: 'App',
  components: { HelloWord, Modal, DogShow },
  setup() {
    onUpdated(() => {
      // console.log('onUpdated');
    });
    const data: DataProps = reactive({
      count: 0,
      increase: () => {
        data.count++;
      },
      double: computed(() => {
        return data.count * 2;
      }),
      numbers: [0, 1, 2],
      person: {},
    });
    const { x, y } = useMousePosition();
    const { result, loading, loaded, error } = useURLLoader<DogResult>('https://dog.ceo/api/breeds/image/random');
    // console.log('result', result);
    const greetings = ref('');
    const updateGreeting = () => {
      greetings.value += 'hello!';
    };
    watch([greetings, () => data.count], (newValue, oldValue) => {
      console.log('new', newValue);
      console.log('old', oldValue);
      document.title = 'updated' + greetings.value + data.count;
    });

    const refData = toRefs(data);
    const modalIsOpen = ref(false);
    const openModal = () => {
      modalIsOpen.value = true;
    };
    const onModalClose = () => {
      modalIsOpen.value = false;
    };
    return { ...refData, updateGreeting, x, y, result, loading, loaded, modalIsOpen, openModal, onModalClose };
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
.dog-img {
  width: 300px;
  height: 300px;
}
</style>
